/**
 * Created by Henry on 15/6/21.
 */
Ext.define('Admin.view.main.module.SimpleGrid', {
    extend: 'Ext.grid.Panel',
    xtype: 'simplegrid',

    requires: ['Ext.ux.GridToolbar'],

    columnLines: true,
    multiSelect: true,
    selType: 'checkboxmodel',
    plugins: [{
        ptype: 'cellediting',
        clicksToEdit: 1
    }, 'gridfilters'],

    initComponent: function () {
        this.model = Admin.view.main.factory.ModelFactory.getModelByName(this.modelName);
        this.store = Ext.create('Admin.store.Base', {
            model: this.model,
            proxy: {
                url: '/api/' + this.url,
                headers: {
                    'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
                }
            }
        });
        this.columns = Admin.view.main.factory.ColumnsFactory.getColumns(this.modelName);
        this.dockedItems = [{
            xtype: 'gridtoolbar'
        }, {
            xtype: 'pagingtoolbar',
            dock: 'bottom',
            store: this.store,
            displayInfo: true,
            plugins: Ext.create('Ext.ux.ProgressBarPager')
        }];
        this.listeners = {
            selectionchange: this.onGridSelectionChange
        };
        this.callParent();
    },

    onGridSelectionChange: function (sm, selected, eOpts) {
        var msg;
        this.down('button#delete').setDisabled(selected.length === 0);
        if (selected.length > 0) {
            msg = "选中\"" + (selected[0].getNameValue()) + "\"等 " + selected.length + " 条记录";
        } else {
            msg = "未选中任何记录";
        }
        return this.up('viewport').getViewModel().set('status', msg);
    }
});